<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 寻找丢失的数字 -->
    <script>
      var missingNumber = function (nums) {
        //数组排序
        nums.sort((a, b) => a - b)
        //赋值
        const n = nums.length
        //遍历数组，判断当前值与索引是否相等
        for (let i = 0; i < n; i++) {
          //如果相等说明是正确的，如果不相等，那就说明当前值缺失了
          if (nums[i] !== i) {
            return i
          }
        }
        return n
      }
      console.log(missingNumber((nums = [9, 6, 4, 2, 3, 5, 7, 0, 1])))
    </script>
  </body>
</html>
